local skel = fk.CreateSkill {
  name = "lb__shenkan",
}

Fk:loadTranslationTable {
  ["lb__shenkan"] = "神勘",
  [":lb__shenkan"] = "结束阶段，若你的「<b>奇兵</b>」有：1.三种类型；2.四种花色。你可以使用所有「<b>奇兵</b>」，然后对至多X名角色各造成X点雷电伤害并弃置剩余「<b>奇兵</b>」（X为达成的项数）。",

  ["#lb__shenkan"] = "神勘：你可以使用所有「<b>奇兵</b>」，然后对至多 %arg 名角色各造成 %arg 点雷电伤害",
  ["#lb__shenkan_damage"] = "神勘：选择至多 %arg 名角色，对这些角色各造成 %arg 点雷电伤害",
  ["#lb__shenkan_use"] = "神勘：请使用一张「<b>奇兵</b>」",

  ["$lb__shenkan1"] = "兵戈，无情！",
  ["$lb__shenkan2"] = "雷霆，在此！",
  ["$lb__shenkan3"] = "煌煌威灵，遵吾敕命。",
  ["$lb__shenkan4"] = "斩无赦！",
}

skel:addEffect(fk.EventPhaseEnd, {
  mute = true,
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and player.phase == Player.Finish then
      local n = 0
      local suit, lb = {}, {}
      for _, id in ipairs(player:getPile("lb__choumou_qibing")) do
        local c = Fk:getCardById(id)
        table.insertIfNeed(suit, c.suit)
        table.insertIfNeed(lb, c.type)
      end
      if #suit > 3 then n = n + 1 end
      if #lb > 2 then n = n + 1 end
      event:setCostData(self, { n = n })
      return n > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb__shenkan:::" .. event:getCostData(self).n })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self).n
    player:broadcastSkillInvoke(skel.name, math.random(1, 2) + ((n == 2) and 1 or 0))
    room:notifySkillInvoked(player, skel.name)
    local cards = player:getPile("lb__choumou_qibing")
    local true_cards = table.filter(cards, function(id)
      return #Fk:getCardById(id):getDefaultTarget(player, { bypass_times = true }) > 0
    end)
    while #true_cards > 0 do
      if room:askToUseRealCard(player, {
            pattern = tostring(Exppattern { id = true_cards }),
            expand_pile = cards,
            prompt = "#lb__shenkan_use",
            cancelable = false,
          }) then
        cards = player:getPile("lb__choumou_qibing")
        true_cards = table.filter(cards, function(id)
          return #Fk:getCardById(id):getDefaultTarget(player, { bypass_times = true }) > 0
        end)
      end
    end
    local tos = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = n,
      prompt = "#lb__shenkan_damage:::" .. n,
      skill_name = skel.name,
    })
    room:sortByAction(tos)
    for _, t in ipairs(tos) do
      if not t.dead then
        room:damage({
          from = player,
          to = t,
          damage = n,
          damageType = fk.ThunderDamage,
          skillName = skel.name,
        })
      end
    end
    cards = player:getPile("lb__choumou_qibing")
    room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "lb__shenkan", "lb__choumou_qibing", true,
      player)
  end,
})

return skel
